package org.zeroes.Events;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.locks.ReentrantLock;
import com.zeroes.cons.*;
import org.zeroes.Components.*;
import com.zeroes.cons.ZSGameManager;

public class ZSEventSheet {
	
	private List<String> names;
	private List<Callback> cbs;
	private List<ZSCondition> conds;
	private List<Condback> makers;
	private ZSGame game;
	private int msAcum=0;
	private final ReentrantLock lock = new ReentrantLock();
	public static final int EVENT_TRIG_ONCE=0;
	public static final int EVENT_TRIG_FOREVER=1;
	public static final int EVENT_TRIG_REPEAT=2;
	public static final int EVENT_TRIG_DURATION=3;
	public static final int EVENT_TRIG_INTERVAL=4;
	public static final int EVENT_TRIG_ANIMATION=5;

	public ZSEventSheet(ZSGame g){
		this.game=g;
		names=Collections.synchronizedList(new ArrayList<String>());	
		cbs=Collections.synchronizedList(new ArrayList<Callback>());	
		
		conds=Collections.synchronizedList(new ArrayList<ZSCondition>());	
		makers=Collections.synchronizedList(new ArrayList<Condback>());	
		
		/*names=new ArrayList<String>();	
		cbs=new ArrayList<Callback>();	
		
		conds=new ArrayList<ZSCondition>();	
		makers=new ArrayList<Condback>();	*/
		
	}
	
public synchronized void on(String c, Callback code)	{
	String[] items = c.split("\\.");
	//ZSGame g = ZSGameManager.AskMe().forCurrentGame();
if (items.length==3&&!items[0].contains("(")&&!items[1].contains("(")&&!items[2].contains("(")){
	
	names.add(c);
	cbs.add(code);
//g.caniterateevents=true;
}
}

public  void tickmadeEvent(int idx, float delta){
	
	ZSCondition _c=conds.get(idx);
	switch(_c.getTrigeringCount()){
	case ZSEventSheet.EVENT_TRIG_FOREVER :
		makers.get(idx).is(_c.name,0);
	break;	
	case ZSEventSheet.EVENT_TRIG_ONCE :
		makers.get(idx).is(_c.name,0);
		makers.remove(idx);
		conds.remove(idx);
	break;	
	case ZSEventSheet.EVENT_TRIG_REPEAT :
		for(int i=0;i<_c.repeats;i++){
			makers.get(idx).is(_c.name,i);
		}
		makers.remove(idx);
		conds.remove(idx);	
	break;	
	case ZSEventSheet.EVENT_TRIG_DURATION :
		if(!_c.stoped){
			if(!_c.paused){
			if(_c.accumdt>=_c.delay&&_c.accumdt<=_c.duration+_c.delay){

			makers.get(idx).is(_c.name,_c.accumdt-_c.delay);

			
			}
			else
			{
				if(_c.accumdt>_c.delay){
					_c.repeated++;
					if(_c.repeated<_c.repeats){
						_c.accumdt=0;
						makers.get(idx).is(_c.name,-1);
					}
					else
					{
						makers.get(idx).is(_c.name,-1);
						makers.remove(idx);
						conds.remove(idx);
					}
				}
			}
			}
			if(!_c.paused)
			_c.addTime(delta);
		}
		else
		{
			makers.remove(idx);
			conds.remove(idx);
		}
	break;	
	case ZSEventSheet.EVENT_TRIG_INTERVAL :
		if(_c.accumdt>=_c.duration){
		makers.get(idx).is(_c.name,_c.accumdt);
		_c.accumdt=0;
		}
		else
		{
			_c.addTime(delta);
		}
break;	
	case ZSEventSheet.EVENT_TRIG_ANIMATION :
		if(!_c.stoped){
		if(_c.frameNumber<_c.frameCount){
		if(_c.accumdt>=_c.duration){
			_c.frameNumber++;
		makers.get(idx).is(_c.name,_c.frameNumber);
		_c.accumdt=0;
		
		}

		}
		else
		{
			//makers.get(idx).is(_c.name,-1);
			_c.repeated++;
			if(_c.repeated<_c.repeats){
				_c.accumdt=0;
				_c.frameNumber=0;
				makers.get(idx).is(_c.name,-1);
			}
			else
			{
				makers.get(idx).is(_c.name,-1);
				makers.remove(idx);
				conds.remove(idx);
			}

		}
		if(!_c.paused)
			_c.addTime(delta);
		}
		else
		{
			makers.remove(idx);
			conds.remove(idx);
		}
break;	
	}
	/*
	if(conds.get(idx).getTrigeringCount()==ZSEventSheet.EVENT_TRIG_FOREVER)
	makers.get(idx).is(conds.get(idx).name);
	else
	{
		makers.get(idx).is(conds.get(idx).name);
		makers.remove(idx);
		conds.remove(idx);
	}*/
	
}

public int getMadeEventsCount(){
	
	return this.conds.size();
}
public  void _(String n, ZSEvent ev){
	
	this.trigger(n,ev );
	
}
public  void _(String ev){
	
	this.trigger(ev, null);
	//anyListener=
}

public void make(String n, Condback c ){
	String[] items = n.split("\\.");
if (items.length==3&&!items[0].contains("(")&&!items[1].contains("(")&&!items[2].contains("(")){
	ZSCondition cond = new ZSCondition(n,ZSEventSheet.EVENT_TRIG_FOREVER);
	conds.add(cond);
	makers.add(c);
	
}
}

public  void makeOnce(String n, Condback c ){
	String[] items = n.split("\\.");
if (items.length==3&&!items[0].contains("(")&&!items[1].contains("(")&&!items[2].contains("(")){
	ZSCondition cond = new ZSCondition(n,ZSEventSheet.EVENT_TRIG_ONCE);
	if(conds.size()==makers.size()){
	conds.add(cond);
	makers.add(c);
	}
}
}

public  void makeCustomRepeat(int count,String n, Condback c ){
	String[] items = n.split("\\.");
if (items.length==3&&!items[0].contains("(")&&!items[1].contains("(")&&!items[2].contains("(")){
	ZSCondition cond = new ZSCondition(n,ZSEventSheet.EVENT_TRIG_REPEAT);
	cond.repeats=count;
	if(conds.size()==makers.size()){
	conds.add(cond);
	makers.add(c);
	}
	
}
}

public  void makeCustomDuration(int duration,String n, Condback c ){
	String[] items = n.split("\\.");
if (items.length==3&&!items[0].contains("(")&&!items[1].contains("(")&&!items[2].contains("(")){
	ZSCondition cond = new ZSCondition(n,ZSEventSheet.EVENT_TRIG_DURATION);
	cond.duration=duration;
	if(conds.size()==makers.size()){
	conds.add(cond);
	makers.add(c);
	}
	
}
}

public  ZSCondition makeCustomDuration(int duration,int delay, int repeats,String n, Condback c ){
	String[] items = n.split("\\.");
if (items.length==3&&!items[0].contains("(")&&!items[1].contains("(")&&!items[2].contains("(")){
	ZSCondition cond = new ZSCondition(n,ZSEventSheet.EVENT_TRIG_DURATION);
	cond.duration=duration;
	cond.delay=delay;
	cond.repeats=repeats;
	if(conds.size()==makers.size()){
	conds.add(cond);
	makers.add(c);
	return cond;
	}
	
}
return null;
}

public  void makeCustomInterval(int interval,String n, Condback c ){
	String[] items = n.split("\\.");
if (items.length==3&&!items[0].contains("(")&&!items[1].contains("(")&&!items[2].contains("(")){
	ZSCondition cond = new ZSCondition(n,ZSEventSheet.EVENT_TRIG_INTERVAL);
	cond.duration=interval;
	if(conds.size()==makers.size()){
	conds.add(cond);
	makers.add(c);
	}
}
}

public  ZSCondition makeCustomInterval(int interval,int totalFrames,int repeats,String n, Condback c ){
	String[] items = n.split("\\.");
if (items.length==3&&!items[0].contains("(")&&!items[1].contains("(")&&!items[2].contains("(")){
	ZSCondition cond = new ZSCondition(n,ZSEventSheet.EVENT_TRIG_ANIMATION);
	cond.duration=interval;
	cond.frameCount=totalFrames;
	cond.repeats=repeats;
	if(conds.size()==makers.size()){
	conds.add(cond);
	makers.add(c);
	return cond;
	}
	
}
return null;
}


private  void trigger(String ev,ZSEvent eve){
	
	String[] items = ev.split("\\.");
	boolean anyListener=false;
	int idx=0;
	for (String cond:this.names){
		anyListener=cond.equals(items[0]+".*.*")||cond.equals(items[0]+"."+items[1]+".*")||cond.equals(items[0]+"."+items[1]+"."+items[2])||cond.equals("*."+items[1]+".*")||cond.equals("*.*."+items[2])||cond.equals(items[0]+".*"+items[2])||cond.equals("*."+items[1]+"."+items[2]);
		if(anyListener){
		
				this.cbs.get(idx).run(ev,eve);

			
		}
		idx++;
	}
	
	for (ZSComponent comp : game.getContainer()){
		anyListener=false;
		 idx=0;
			for (String cond:comp.getNamesList()){
				anyListener=cond.equals(items[0]+".*.*")||cond.equals(items[0]+"."+items[1]+".*")||cond.equals(items[0]+"."+items[1]+"."+items[2])||cond.equals("*."+items[1]+".*")||cond.equals("*.*."+items[2])||cond.equals(items[0]+".*"+items[2])||cond.equals("*."+items[1]+"."+items[2]);
				if(anyListener){
				
						comp.getListenersList().get(idx).run(ev,eve);

					
				}
				idx++;
			}
		
	}
	
}

public int getSizeofSheet(){
	return this.cbs.size();
	
}

}
